1 using UnityEngine;
2 using
System.Collections;
3
4 public
class EnemyAttack : MonoBehaviour {
5
6     
public int attackDamage = 60;
7     
public float timeBetweenAttacks = 0.5f;
8     
// todo: grab the player's health script
9     
bool playerInRange = false;
10     
float timer;
11
12
13     Animator anim;
14     GameObject player;
15     EnemyHealth enemyHealth;
16     PlayerHealth playerHealth;
17     Animator playerAnim;
18     
bool isEnabled = true;
19     UnityEngine.AI.NavMeshAgent agent;
20     PlayerShoot playerShoot;
21     IronManBehaviorScript playerMovement;
22
23
24     
// Use this for initialization
25     
void Start () {
26         anim = GetComponent<Animator> ();
27         player = GameObject.FindGameObjectWithTag (
"Player");
28         enemyHealth = GetComponent<EnemyHealth> ();
29         playerHealth = player.GetComponent<PlayerHealth> ();
30         agent = GetComponent<UnityEngine.AI.NavMeshAgent> ();
31         playerShoot = player.GetComponent<PlayerShoot> ();
32         playerMovement = player.GetComponent<IronManBehaviorScript> ();
33     }
34     
35     
// Update is called once per frame
36     
void Update () {
37
38         
if (isEnabled == false)
39             
return;
40         
41         timer += Time.deltaTime;
42         
if (timer >= timeBetweenAttacks && playerInRange == true && enemyHealth.currentHealth > 0) {
43             Attack ();
44         }
45
46         
if (playerHealth.currentHealth <= 0) {
47             playerHealth.Death();
48
49             isEnabled =
false;
50             anim.enabled =
false;
51             
//anim.SetTrigger ("idle");
52             agent.enabled =
false;
53             playerShoot.DisableShooting ();
54             playerMovement.DisableMovement ();
55         }
56
57     
58     }
59
60     
// whenever we get close to the player, we can attack
61     
void OnTriggerEnter(Collider other){
62         
if (other.gameObject == player) {
63             playerInRange =
true;
64         }
65     }
66
67
68     
void OnTriggerExit(Collider other){
69         
if (other.gameObject == player) {
70             playerInRange =
false;
71         }
72     }
73
74
75     
void Attack(){
76         timer =
0f;
77         anim.SetTrigger (
"Attack");
78         
if (playerHealth.currentHealth > 0) {
79             playerHealth.TakeDamage (attackDamage);
80         }
81     }
82 }


Gõ tìm kiếm nhanh...